/**
 * \file kernel/driver/fs/tmpfs/tmpfs.c
 * \author Davide Gessa
 * \date 12/22/2009
 */
#include "tmpfs.h"
#include <driver.h>
#include <fs/fs.h>


driver_t driver_tmpfs =
{
	.name = "tmpfs\0",
	
	.type = DRV_TYPE_FS,
	.fs.mount = tmpfs_mount,
	.fs.umount = tmpfs_umount,
	.fs.rmdir = tmpfs_rmdir,
	.fs.mkdir = tmpfs_mkdir
};



/**
 * Umonta il tmpfs
 */
uint32_t tmpfs_umount(inode_t *root)
{
	//root->childs = NULL;

	return MOUNT_STATE_OK;
}



/**
 * Monta il tmpfs
 */
uint32_t tmpfs_mount(char *dev, uint32_t flags, inode_t *root)
{
	//root->childs = NULL;

	return MOUNT_STATE_OK;
}



/**
 * Crea un nodo dir
 */
uint32_t tmpfs_mkdir(char *name, inode_t *root)
{
	if(root->type != NODE_TYPE_DIR)
		return 0;
		
	inode_t *node = fs_create_node(name, root, NODE_TYPE_DIR, 0);
	
	node->next = root->childs;
	root->childs = node;
	
	return node->inode_n;
}





/**
 * Elimina un nodo dir
 */
uint32_t tmpfs_rmdir(char *name, inode_t *root)
{
	if(root->type != NODE_TYPE_DIR)
		return 0;
		
	inode_t *c = root->childs;
	inode_t *p = NULL;
	uint32_t in;
	
	while(c != NULL)
	{
		if(strcmp(c->name, name) == 0)
		{
			if(p != NULL)
				p->next = c->next;
			else
				root->childs = root->childs->next;
				
			in = c->inode_n;
			free(c);
			return in;
		}
		p = c;
		c = c->next;
	}
	
	return 0;
}
